<%@ page language="java" import="java.util.*,com.xq.shop.po.Role" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<base href="<%=basePath%>" />
<jsp:include page="/jsp/admin/common.jsp" />
<jsp:include page="/jsp/formHead.jsp" />
<jsp:include page="/jsp/listHead.jsp" />
<link rel="stylesheet" type="text/css" href="<%=basePath%>resource/admin_css/product.css" />
<script type="text/javascript">
$().ready(function(){

	var default_PID = "${entity.parent_id}" || "0";

	$("select[name='parent_id']").val(default_PID);
		
	$("#backButton").click(function(){
		location.href = basePath + "jsp/admin/product/category_list.jsp";
	});
	
	$("#form_edit").validate({	//验证配置
		rules : {
			name : "required",
			seq : {
				required : true,
				digits : true
			}
		},
		messages : {
			parent_id : {required : "必选"}
		}
	});

	var $listbox = QListBox.init("#parent_id", {css:{width: "188px"}, defaultItem:{text:"顶级分类",value:0}}),
		list = [];
	<c:forEach var="category_1st" items="${categoryList}">
		list.push({value: "${category_1st.id }", text: "${category_1st.name }", "class" : "category_1st"});
	</c:forEach>
	$listbox.refresh(list);
	$listbox.onSelected(function(_value){
		$("input[name='parent_id']").val(this.getValue());
	});

	//判断是否是编辑状态
	var in_edit = ${!empty entity.id};
	
	if(in_edit){
		$("div.path").append("<span>商品分类修改</span>");
		$listbox.setValue("${entity.parent_id }");
	} else {
		$("div.path").append("<span>商品分类创建</span>");
	}

	var default_ParamSize = "${fn:length(entity.params) }" || "0";
	$("#addParamBtn").click(function(){
		var $input = $("<input type='text' class='text' maxlength='200' />").attr("name", "params[" + default_ParamSize++ +"].name"),
			$delBtn = $("<input type='button' name='delParamBtn' value='删除属性'/>"),
			$td = $("<td/>").append($input).append($delBtn),
			$tr = $("<tr />").append($("<th>属性:</th>")).append($td);
		$delBtn.click(function(){
			$(this).closest("tr").remove();
		});
		$tr.insertBefore($("table.input tr:last"));
	});
	$("input[name='delParamBtn']").click(function(){
		$(this).closest("tr").remove();
	});
});
</script>
</head>
<body>
	<div class="path">
		<a href="">首页</a> &raquo;
	</div>
	<form id="form_edit" method="post" action="<%=basePath %>admin/category_save.do">
		<table class="input tabContent">
			<tr>
				<th>
					<span class="requiredField">*</span>名称:
				</th>
				<td>
					<input type="hidden" name="id" value="${entity.id }"/>
					<input type="text" name="name" class="text" maxlength="200" value="${entity.name}"/>
				</td>
			</tr>
			<tr>
				<th>
					<span class="requiredField">*</span>上级分类:
				</th>
				<td>
					<div id="parent_id"></div>
					<input name="parent_id" type="hidden" value="0"/>
				</td>
			</tr>
			<tr>
				<th>
					<span class="requiredField">*</span>顺序:
				</th>
				<td>
					<input type="text" name="seq" class="text" maxlength="200" value="${entity.seq }"/>
				</td>
			</tr>
			<tr>
				<th>
				</th>
				<td>
					<input type="button" id="addParamBtn" value="添加属性"/>
				</td>
			</tr>
			<c:forEach var="p" items="${entity.params }" varStatus="status">
			<tr>
				<th>
					属性:
				</th>
				<td>
					<input type="text" name="params[${status.index}].id" class="hidden" maxlength="200" value="${p.id }"/>
					<input type="text" name="params[${status.index}].name" class="text" maxlength="200" value="${p.name }"/>
					<input type="button" name="delParamBtn" value="删除属性"/>
				</td>
			</tr>
			</c:forEach>
			<tr>
				<th>
					&nbsp;
				</th>
				<td>
					<input type="submit" class="button" value="确定" />
					<input type="button" id="backButton" class="button" value="返回"/>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>